import PlayerModel, { IPlayer } from '../model/player.model'

class PlayerService {
    /**
     * 从数据库中读取管理员信息
     */
    public async getPlayerInfo(username: string): Promise<IPlayer | null> {
        const player = await PlayerModel.findOne({ username })
        return player
    }

    /**
     * 获取Keqingtoken
     */
    public async getKeqingToken(): Promise<string | null> {
        const player = await PlayerModel.findOne({ username: 'keqing' })
        return player ? player.token : null
    }

    /**
     * 写入token
     * @param username
     * @param token
     */
    public async setToken(username: string, token: string) {
        const player = await PlayerModel.findOne({ username })
        if (player) {
            player.token = token
            await player.save()
        } else {
            await PlayerModel.create({ username, token })
        }
    }
}

export default new PlayerService()
